package com.aiit.controller;

import com.aiit.pojo.*;
import com.aiit.service.IDoctorService;
import com.aiit.service.impl.ConService;
import com.aiit.service.impl.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * ClassName:DoctorController
 * Package:com.aiit.controller
 * Description:
 *
 * @Date:2020/4/18 13:52
 * @Author:xjs
 */
@Slf4j
@Controller
public class DoctorController {
    @Resource
    private IDoctorService doctorservice;
    @Resource
    private UserService userService;
    @Resource
    private ConService conService;

    @PostMapping("doctorUser/update")
    @ResponseBody
    public ProcResult doctorUpdateByDoctor(@RequestBody Doctor doctor){
        System.out.println(doctor);
        doctorservice.doctorUpdate(doctor);
        return ProcResult.success();
    }
    @RequestMapping("/doctor/doctorUser")
    public String doctorUser(Model model){
        int totals=userService.addTotal();
        model.addAttribute("totals",totals);
        return "doctor/doctorUser";
    }

    @Transactional
    @RequestMapping("/doctor/findname")
    @ResponseBody
    public ProcResult doctorByName(String username){
        int userid=userService.findNameByid(username);
        Doctor doctor=doctorservice.selectDoctorById(userid);
        System.out.println(doctor);
        return ProcResult.success().bind("doctor",doctor);
    }
    @RequestMapping("/doctor/patients")
        public String patientsList(Model model){

        List<Patient> list=userService.doctorFindPatients();
        System.out.println("list:"+list);
        model.addAttribute("list",list);
        int totals=userService.addTotal();
        model.addAttribute("totals",totals);
        return "doctor/patients";
    }
    @PostMapping("/doctor/reply")
    @ResponseBody
    public ProcResult doctorRely(Consultation consultation){
        log.info("======>consultation:"+ consultation);
       int num= conService.updateRelpy(consultation);
       if(num>0){
           return  ProcResult.success();
       }
        return  ProcResult.fail();
    }

    /* 管理员*//*
    @GetMapping("/admin/doctor")
    public String doctorList(Model model){
    List<DoctorbyDept> list=doctorservice.findAll();
        System.out.println(list);
        model.addAttribute("doctorList",list);
        return "admin/doctor";
    }
    @GetMapping("/admin/findDoctor")
    public String findDoctor(){
        return "admin/doctor/findDoctor";
    }
    @PostMapping("/admin/findDoctor")
    public String findDoctor(String name,String idnumber,Model model){

        if(name.equals("") && idnumber.equals("")){
            System.out.println("输入为空！");
            List<DoctorbyDept> list=doctorservice.findAll();
            model.addAttribute("findDoctor", list);
            return "admin/doctor/findDoctor";
        }else {
            List<DoctorbyDept> list=doctorservice.findDoctorById(name,idnumber);
            model.addAttribute("findDoctor", list);
            for (DoctorbyDept c : list) {
                System.out.println(c.getId() + "\t" + "||" + c.getExpert() + "\t" + "||" + "\t" + c.getName() + "\t" + "||" + "\t" + c.getAge() + "\t" + "||" + "\t" + c.getSex());
            }

            System.out.println(idnumber);
            System.out.println(name);

            return "admin/doctor/findDoctor";
        }

    }
    @GetMapping("/admin/doctor/doctorAdd")
    public String doctorAdd(){
        return "admin/doctor/doctorAdd";
    }
    @PostMapping("/admin/doctor/doctorAdd")
    public String doctorAdd(Doctor doctor, HttpServletResponse resp) throws IOException {
        int result=doctorservice.addDoctor(doctor);
        if(result>0) {
            System.out.println("添加医生成功！");
            resp.setContentType("text/html;charset=GBK");
            PrintWriter out = resp.getWriter();
            String msg="添加成功！";
            String url="/admin/doctor";
            out.println("<script>");
            out.println("alert('" + msg + "');");

            out.println("</script>");
        }
        return "admin/doctor/doctorAdd";
    }
    @GetMapping("/admin/doctor/doctorUpdate")
    public String doctorUpdate() {
        return "admin/doctor/doctorDetail";
    }
    @PostMapping("/admin/doctor/doctorUpdate")
    public String doctorUpdate(Doctor doctor, HttpServletResponse resp) throws IOException {
        int result=doctorservice.doctorUpdate(doctor);
        if(result>0){
            System.out.println("信息更新成功！");

        }
        return "admin/doctor/doctorDetail";
    }
    @GetMapping("/admin/doctor/doctorDelete")
    public String doctorDelete(){
        return "admin/doctor";
    }
    @PostMapping("/admin/doctor/doctorDelete")
    public String doctorDelete(Doctor doctor){

        return "admin/doctor";
    }*/
}
